#!/bin/bash
INPUTFILE=${1}
EXPERIMENT=${2}

PLOTTMP=plot.dat

csv2dat() {
	sed -e 's/;//g'
}

tailafter() {
	local grepline=${1}
	local lines=${2}
	grep -A ${lines} "${grepline}" | tail -n${lines}
}

getValue() {
	local filename=${1}
	local name=${2}
	value=$(grep "^${name}:.*" ${filename}|cut -f2 -d':')
	echo ${value}
}

argsjoin() {
	args=( "${@}" )
	( IFS=,; echo "${args[*]}" ) |sed -e 's/,/, /g'
}

if [ -n "${PNGOUT}" ]; then
	# If desired, create pngs.
	TERMINAL="png"
	EXTENSION="png"
else
	# By default, print into eps.
	TERMINAL="postscript eps enhanced; set size 0.5,0.5"
	EXTENSION="eps"
fi

plotit() {
	local out="${1}"
	local graphname="${2}"
	local pre="${3}"
	shift 3
	local plotlines=()
	local plotline=
	while [ ${#} -gt 0 ]; do
		local data="${1}"
		shift 1
		plotline="\"${PLOTTMP}\" ${data}"
		plotlines=( "${plotlines[@]}" "${plotline}" )
	done
	eval gnuplot -e "'"set terminal ${TERMINAL}\; set title \"${graphname}\"\; set output \"${out}.${EXTENSION}\"\; ${pre} plot $(argsjoin "${plotlines[@]}")"'"
}

experiment1() {
	local size=$(getValue "${INPUTFILE}" "Time limit")
	echo "Time limit: ${size}"
	cat ${INPUTFILE} | tailafter 'Day; Age; Episodes created; Episodes total; Episodes remembered at level 0; 1; 2; 3; Total; Compression Ratio;' ${size} | csv2dat > ${PLOTTMP}
	plotit "Experiment1Graph1" \
			"Memory volume progression by level" \
			"set log x; set xrange [2:]; set yrange [0:]; set xlabel \"Day of simulation\"; set ylabel \"Episodes remembered\";" \
			"using 1:5 smooth csplines title \"Days\" with lines" \
			"using 1:6 smooth csplines title \"Weeks\" with lines" \
			"using 1:7 smooth csplines title \"Months\" with lines" \
			"using 1:8 smooth csplines title \"Years\" with lines"
	plotit "Experiment1Graph2" \
			"Ratio of real episodes and remembered episodes" \
			"set log x; set xlabel \"Day of simulation\"; set ylabel \"Episodes remembered ratio\";" \
			"using 1:10 smooth csplines title \"\" with lines"
	cat ${INPUTFILE} | tailafter 'Day; Age; Created episodes; Episodes remaining at level 0; 1; 2; 3;' ${size} |csv2dat > ${PLOTTMP}
	plotit "Experiment1Graph3" \
			"Memories retained at different chronobag levels" \
			"set log x; set xrange [2:]; set yrange [0:]; set xlabel \"Memory Age\"; set ylabel \"Episodes remembered\";" \
			"using 2:4 smooth csplines title \"Days\" with lines" \
			"using 2:5 smooth csplines title \"Weeks\" with lines" \
			"using 2:6 smooth csplines title \"Months\" with lines" \
			"using 2:7 smooth csplines title \"Years\" with lines"
}

experiment3() {
	local size=99999
	cat ${INPUTFILE} | tailafter 'Offset; w/o temporal schemas;  w/ temporal schemas; w/o temporal schemas; w/ temporal schemas' ${size} | csv2dat > ${PLOTTMP}
	plotit "Experiment3Graph1" \
			"Dating errors of weekday-specific episodes without temporal schemas" \
			"set xrange [-75:75]; set yrange [0:]; set xlabel \"Days\"; set ylabel \"Episodes\";" \
			"using 1:2 title \"Number of episodes\" with lines"
	plotit "Experiment3Graph2" \
			"Dating errors of weekday-specific episodes with temporal schemas" \
			"set xrange [-75:75]; set yrange [0:]; set xlabel \"Days\"; set ylabel \"Episodes\";" \
			"using 1:3 title \"Number of episodes\" with lines"
	plotit "Experiment3Graph3" \
			"Dating errors of weekday-unspecific episodes without temporal schemas" \
			"set xrange [-75:75]; set yrange [0:]; set xlabel \"Days\"; set ylabel \"Episodes\";" \
			"using 1:4 title \"Number of episodes\" with lines"
	plotit "Experiment3Graph4" \
			"Dating errors of weekday-unspecific episodes with temporal schemas" \
			"set xrange [-75:75]; set yrange [0:]; set xlabel \"Days\"; set ylabel \"Episodes\";" \
			"using 1:5 title \"Number of episodes\" with lines"
}

experiment4() {
	for i in 10 100 1000 10000; do
        cat ${INPUTFILE} | tailafter "^Total volume by age (${i})" "$((${i} + 1))" | csv2dat > ${PLOTTMP}
		plotit "Experiment4Graph${i}" \
				"Retention curve" \
				"set log x; set yrange [0:]; set xlabel \"Age\"; set ylabel \"Episodes\";" \
				"smooth csplines title \"${i} Days\" with lines"
	done
}

case ${EXPERIMENT} in
	(1) experiment1;;
	(3)	experiment3;;
	(4) experiment4;;
esac
rm -f "${PLOTTMP}"

